<?php

namespace App\Http\Controllers\Admin\V1;

use App\Exceptions\ValidationException;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    //
    // public function __construct()
    // {
    //
    // }

    /**
     * @param Request $request
     * @param array $rules
     * @param array $messages
     * @param array $customAttributes
     * @return array
     * @throws ValidationException
     * @Depiction 自定义验证返回信息，去掉则为默认返回
     * @Author hwy <429208948@qq.com>
     * @Date 2022/1/7  17:48
     */
    public function validate(Request $request, array $rules, array $messages = [], array $customAttributes = [])
    {
        $validator = $this->getValidationFactory()->make($request->all(), $rules, $messages, $customAttributes);

        if ($validator->fails()) {
            // 默认返回
            // $this->throwValidationException($request, $validator);
            // 改为抛出第一个错误
            throw new ValidationException($validator->errors()->first());
        }

        return $this->extractInputFromRules($request, $rules);
    }

    /**
     * @param string $message
     * @param array $data
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     * @Depiction 成功返回
     * @Author hwy <429208948@qq.com>
     * @Date 2022/1/7  17:13
     */
    protected function responseSuccessData($message = '请求成功！', $data = [], $code = 0, $statusCode = 200)
    {
        $res = response_success_msg($message, $data, $code);

        return response()->json($res, $statusCode);
    }

    /**
     * @param string $message
     * @param array $data
     * @param int $code
     * @return \Illuminate\Http\JsonResponse
     * @Depiction 错误返回
     * @Author hwy <429208948@qq.com>
     * @Date 2022/1/7  17:13
     */
    protected function responseErrorData($message = '请求失败！', $data = [], $code = -1, $statusCode = 400)
    {
        $res = response_error_msg($message, $data, $code);

        return response()->json($res, $statusCode);
    }
}
